package DataStore
{
	import BasicEngine.Dispatcher;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Shader;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import GameScheduler.Game;
    import Enums.*;
	public class ResourceManager
	{
		public static var SoliderPool:Dictionary;
				
		public static var ImgLoader:Loader;
		public static var ImgLoadList:Vector.<ImgLoadTask>;
		public static var LoadLock:Boolean;
		public static var CurrentTask:ImgLoadTask;		
		
		public static var UrlLoader:URLLoader;
		public static var UrlLoadList:Vector.<UrlLoadTask>;
		public static var UrlLoadLock:Boolean;
		public static var CurrentUrlTask:UrlLoadTask;
		
		
		public static var Shaders:Vector.<Shader>;		
		private static var baseSoliderNums:int = 38;
		
		private static var timeCounter:int ;
		private static var urlTimeCounter:int;
		
		public function ResourceManager()
		{
		}	
					
		public static function Init():void
		{
			ImgLoader = new Loader();
			SoliderPool = new Dictionary();
			ImgLoadList = new Vector.<ImgLoadTask>();
			LoadLock = false;
			Shaders = new Vector.<Shader>();
			CurrentTask = null;
			
			UrlLoader = new URLLoader();
			UrlLoadList = new Vector.<UrlLoadTask>();
			UrlLoadLock = false;
			
			for (var i:int = 0; i < baseSoliderNums; i++)
			{
				SoliderPool[i] = new Dictionary();
			}
		}
		
		public static function AddImgLoadTask(_task:ImgLoadTask):void
		{
			ImgLoadList.push(_task);
		}
		
		public static function AddImgLoadTaskEle(_type:String, _url:String, _name:String, _parse:Function):void
		{
			var task:ImgLoadTask = new ImgLoadTask(_url, _type, _name, _parse);
			ImgLoadList.push(task);
		}
		
		public static function AddUrlLoadTask(_url:String,_parseUrlLoader:Function):void
		{
			var urlTask:UrlLoadTask = new UrlLoadTask(_url, _parseUrlLoader);
			UrlLoadList.push(urlTask);
		}
		
		public static function ExecuteLoadTask():void
		{			
			if (ImgLoadList.length > 0)
			{
				timeCounter += Dispatcher.Clock.Tick;
				if (!LoadLock)
				{
				   CurrentTask = ImgLoadList.shift();
				   ImgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnParseLoad);
				   ImgLoader.name = CurrentTask.name + "," + CurrentTask.type;
				   ImgLoader.load(new URLRequest(CurrentTask.url));
				   LoadLock = true;
				}
			}	
			else
			{
				timeCounter = 0;
			}
			
			if (UrlLoadList.length > 0)
			{
				urlTimeCounter += Dispatcher.Clock.Tick;
				if (!UrlLoadLock)
				{
					urlTimeCounter += Dispatcher.Clock.Tick;
				    CurrentUrlTask = UrlLoadList.shift();
				    UrlLoader.addEventListener(Event.COMPLETE, OnParseUrlLoad);
				    UrlLoader.dataFormat = URLLoaderDataFormat.BINARY;
				    UrlLoader.load(new URLRequest(CurrentUrlTask.url));
				    UrlLoadLock = true;
				}				
			}
			else
			{
				urlTimeCounter = 0;
			}
		}		
		
		public static function OnParseShader(_data:ByteArray):void
		{
			var shader:Shader = new Shader(_data);
			Shaders.push(shader);
		}
		
		public static function OnParseLargeImg(_bmpData:BitmapData, _name:String , _type:String):void
		{
			if (_type == "Atk")
			{
			    ParseAtk(_bmpData,baseSoliderNums);		    
			}
			if (_type == "Mov")
			{
			    ParseMov(_bmpData,baseSoliderNums);
			}
			if (_type == "Spc")
			{
			    ParseSpc(_bmpData,baseSoliderNums);   
			}
		}
		
		public static function OnParsePlayer(_bmpData:BitmapData,_name:String ,_type:String):void
		{
			if (_type ==  LoadType.Solider)
			{
				if (SoliderPool[_name] == null)   
				{
				     var soliderList:Dictionary = new Dictionary();
				    SoliderPool[_name] = soliderList;	 
				}
				var soliderTiles:Dictionary =  SoliderPool[_name] ;
		         if (_bmpData.width == 64)//atk
		         {				
		                 for (var a:int = 0; a < 12; a++)
		            	 {
		            		 var atk:BitmapData = new BitmapData(64, 64);
		            		 atk.copyPixels(_bmpData, new Rectangle(0, 64 * a, 64, 64), new Point(0, 0));		
		            		 soliderTiles[a] = atk;
		            	 }
		         }
		         else if (_bmpData.height == 240)//spc
		         {				
		            	for (var s:int = 0; s < 5; s++)
		            	{
		            		var spc:BitmapData = new BitmapData(48, 48);
		            		spc.copyPixels(_bmpData, new Rectangle(0, 48 * s, 48, 48), new Point(0, 0));		
		            		soliderTiles[12+s] = spc;
		            	}
		         }
		         else//Mov
		          {
		               for (var m:int = 0; m < 11; m++)
		            	{
		            		var mov:BitmapData = new BitmapData(48, 48);
		            		mov.copyPixels(_bmpData, new Rectangle(0, 48 * m, 48, 48), new Point(0, 0));
		            		soliderTiles[17+m] = mov;
		            	}
		          }
			}
		}
		
		public static function GetFlag():Boolean
		{
			return LoadLock;
		}
				
		private static function OnParseLoad(e:Event):void
		{
			LoadLock = false;
			timeCounter = 0;
			var loader2:Loader = (e.target.loader) as Loader;			
			var bmpData:BitmapData = (loader2.content as Bitmap).bitmapData;			
			var arr:Array = loader2.name.split(",");
			var name:String = arr[0];
			var type:String = arr[1];
			CurrentTask.parseLoad(bmpData, name, type);
			loader2.contentLoaderInfo.removeEventListener(Event.COMPLETE, OnParseLoad);
		}		
		
		private static function OnParseUrlLoad(e:Event):void
		{
			UrlLoadLock = false;
			urlTimeCounter = 0;			
			var urlLoader:URLLoader = e.target as URLLoader;
			CurrentUrlTask.parseLoad(urlLoader.data);
			urlLoader.removeEventListener(Event.COMPLETE, OnParseUrlLoad);			
		}		
		
		private static function ParseAtk(_bmpData:BitmapData,_l:int):void
		{
			for (var i:int = 0; i < _l; i++)
			    {
			    	for (var j:int = 0; j < 12; j++)
			    	{
			    	   var atk:BitmapData = new BitmapData(64, 64);
					   var dy:int = int(i / 19) * (64 * 12);
					   var dx:int = i % 19;
			    	   atk.copyPixels(_bmpData, new Rectangle(dx * 64, j*64 + dy, 64, 64), new Point(0, 0));
			    	   SoliderPool[i][j] = atk;
			    	}
			    }	
		}
		
		private static function ParseMov(_bmpData:BitmapData,_l:int):void
		{
			for (var i:int = 0; i < _l; i++)
			    {
			    	for (var j:int = 0; j < 11; j++)
			    	{
			    		var mov:BitmapData = new BitmapData(48, 48);
			    		mov.copyPixels(_bmpData, new Rectangle(i * 48, j * 48, 48, 48), new Point(0, 0));
			    		SoliderPool[i][j+12] = mov;
			    	}
			    }
		}
		
		private static function ParseSpc(_bmpData:BitmapData,_l:int):void
		{
			for (var i:int = 0; i < _l; i++)
			    {
			    	for (var j:int = 0; j < 5; j++)
			    	{                                   
			    	   var spc:BitmapData = new BitmapData(48, 48);
			    	   spc.copyPixels(_bmpData, new Rectangle(i * 48, j * 48, 48, 48), new Point(0, 0));
			    	   SoliderPool[i][j+23] = spc;
			    	}
			    }			
		}
	}
}